﻿Imports System.ComponentModel.DataAnnotations
Imports Models.Security

Public Class Rols
    Inherits BaseService

    Sub New(request As System.Net.Http.HttpRequestMessage)
        Me.loadToken(request)
    End Sub
    Sub New(token As String)
        Me.token = token
    End Sub

    ''' <summary>
    ''' Gets the specified id.
    ''' </summary>
    ''' <param name="id">The id.</param>
    ''' <returns></returns>
    Public Function [Get](id As Integer) As RolVO
        Try
            Me.checkAccess("GETROL")
            Dim oEntity As RolVO

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                Dim oDataEntity As Data.BaseModel.Rol = (From u In oDB.Rols Where u.RolId = id Select u).FirstOrDefault()

                If oDataEntity IsNot Nothing Then
                    oEntity = New RolVO(oDataEntity)
                End If
            End Using

            Return oEntity

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try


    End Function


    ''' <summary>
    ''' Get List of Rols
    ''' </summary>
    ''' <param name="companyId">optional for filter</param>
    ''' <returns>Array of Solutions</returns>
    ''' <remarks></remarks>
    Public Function List(Optional companyId As Integer = -1) As IEnumerable(Of RolVO)
        Try
            Me.checkAccess("LISTROLS")
            Dim oResult As New Generic.List(Of RolVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Rol)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                If Not companyId = -1 Then
                    oDataEntity = (From c In oDB.Rols Where c.CompanyId = companyId Select c).ToList

                Else
                    oDataEntity = oDB.Rols.ToList()

                End If

                If oDataEntity IsNot Nothing Then
                    For Each dbData As Data.BaseModel.Rol In oDataEntity
                        oResult.Add(New RolVO(dbData, True))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Update a rol
    ''' </summary>
    ''' <param name="rol"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Update(rol As RolVO) As Integer
        Try
            Me.checkAccess("UPDATEROL")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oDataSolution As Data.BaseModel.Rol = (From c In oDB.Rols Where c.RolId = rol.rolId Select c).FirstOrDefault()

                If oDataSolution IsNot Nothing Then

                    If Not String.IsNullOrEmpty(rol.name) Then
                        oDataSolution.Name = rol.name
                    End If
                    If Not String.IsNullOrEmpty(rol.code) Then
                        oDataSolution.Code = rol.code
                    End If
                    '' todo: actualizar listado de procesos

                    nRes = oDB.SaveChanges()

                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' Create a rol
    ''' </summary>
    ''' <param name="rol"></param>
    ''' <returns>The new ID  of the entity</returns>
    ''' <remarks></remarks>
    Public Function Create(rol As RolVO) As Integer
        Try
            Me.checkAccess("CREATEROL")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As New Data.BaseModel.Rol()
                If oData IsNot Nothing Then
                    If rol.companyId > 0 Then
                        oData.CompanyId = rol.companyId

                        If Not String.IsNullOrEmpty(rol.name) Then
                            oData.Name = rol.name
                        End If
                        If Not String.IsNullOrEmpty(rol.code) Then
                            oData.Code = rol.code
                        End If

                    End If

                    oDB.Rols.AddObject(oData)
                End If
                If oDB.SaveChanges() > 0 Then
                    nRes = oData.RolId
                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' Delete a Rol
    ''' </summary>
    ''' <param name="id">rolId</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Delete(id As Integer) As Integer
        Try
            Me.checkAccess("DELETEROL")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As Data.BaseModel.Rol = (From c In oDB.Rols Where c.RolId = id Select c).FirstOrDefault()

                If oData IsNot Nothing Then
                    oDB.DeleteObject(oData)
                    nRes = oDB.SaveChanges()
                End If

            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

End Class